package com.example.lijinyuan.baseandroid.Service;

import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;

import com.example.lijinyuan.baseandroid.R;

/**
 * Created by lijinyuan on 15/11/28.
 */
public class BindService extends Activity {


    ToBindService service;
    Intent intent=new Intent();
    Button start,stop;
    ServiceConnection conn = new ServiceConnection() {

        @Override//当服务跟启动源断开的时候 会自动回调
        //service 崩溃或者被强杀。
        public void onServiceDisconnected(ComponentName name) {
            // TODO Auto-generated method stub

        }

        @Override//当服务跟启动源连接的时候 会自动回调
        public void onServiceConnected(ComponentName name, IBinder binder) {
            // TODO Auto-generated method stub
            service = ((ToBindService.MyBindService)binder).getService();
        }
    };
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.service_my_service);

        start= (Button) findViewById(R.id.start_service);
        stop= (Button) findViewById(R.id.stop_service);
        intent.setClass(BindService.this,ToBindService.class);
        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bindService(intent, conn, Service.BIND_AUTO_CREATE);
            }
        });
        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                unbindService(conn);
            }
        });
    }

    public void doClick (View view){

        switch (view.getId()){


            case R.id.play:
                service.Play();
                break;
            case R.id.pause:
                service.Pause();
                break;
            case R.id.pervious:
                service.Pervious();
                break;
            case R.id.next:
                service.next();
                break;
        }
    }
}
